﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HCFramework.API.LINQ;
using System.IO;

namespace HCFramework.Product.FrontEndControl
{
    public partial class ListProduct : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            LoadProduct();
        }

        private void LoadProduct()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Products
                         where a.IsActive
                         select a;

            // filter by category
            if (M.UrlQuery.categoryID.HasValue)
            {
                ProductCategory cat = C.ProductCategoryController.GetByID(M.UrlQuery.categoryID.Value);
                this.Page.Title = M.Common.GetLang(cat.Name);
                List<int> listChildCatID = C.ProductCategoryController.GetAllChild(cat).Select(ik => ik.ProductCategoryID).ToList();
                listChildCatID.Add(cat.ProductCategoryID);
                result = from r in result
                         where listChildCatID.Contains(r.CategoryID)
                         select r;
            }

            // filter by keyword
            if (M.UrlQuery.search != "")
            {
                result = from r in result
                         where r.ProductName.ToLower().Replace(" ", "").Contains(M.UrlQuery.search.ToLower().Replace(" ", ""))
                         select r;
            }

            // filter by PropertyFilter
            if (M.UrlQuery.filters.Count > 0)
            {
                List<int> listProductId = C.Product_PropertyFilterController.GetList(M.UrlQuery.filters).Select(ik => ik.ProductID).ToList();
                result = from r in result
                         where listProductId.Contains(r.ProductID)
                         select r;
            }

            // filter by Manufacturer
            if (M.UrlQuery.manufacturerID.HasValue)
            {
                result = from r in result
                         where r.ManufacturerID == M.UrlQuery.manufacturerID
                         select r;
            }

            // sort
            if (M.UrlQuery.sort == "1" || M.UrlQuery.sort == "")
            {
                result = from r in result
                         orderby r.DateTimeInsert descending
                         select r;
            }
            if (M.UrlQuery.sort == "2")
            {
                result = from r in result
                         orderby r.DateTimeInsert
                         select r;
            }
            if (M.UrlQuery.sort == "3")
            {
                result = from r in result
                         orderby r.Price
                         select r;
            }
            if (M.UrlQuery.sort == "4")
            {
                result = from r in result
                         orderby r.Price descending
                         select r;
            }

            // Paging
            int itemPerPage = 16;
            int currentPage = 1;
            if(M.UrlQuery.page.HasValue) currentPage = M.UrlQuery.page.Value;
            litPaging.Text = M.Common.GetPaging(result.Count(), itemPerPage, ref currentPage);
            result = result.Skip((currentPage - 1) * itemPerPage).Take(itemPerPage);

            List<HCFramework.API.LINQ.Product> listProduct = result.ToList();
            foreach (HCFramework.API.LINQ.Product product in listProduct)
            {
                litProducts.Text += GetDisplayItem(product);
            }
        }

        private string GetDisplayItem(API.LINQ.Product product)
        {
            string temp = File.ReadAllText(Server.MapPath(@"~\Product\FrontEndControl\Template\ProductItem.htm"));
            return M.Template.MergeProduct(temp, product);
        }
    }
}